package com.old.people.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.old.people.entities.Appraisal;
import com.old.people.entities.Disease;
import com.old.people.entities.OldPeople;
import com.old.people.entities.OrderServiceServicer;
import com.old.people.entities.ServiceServicer;
import com.old.people.pay.AlipayBean;
import com.old.people.service.PayService;
import com.old.people.service.UserServices;
import com.old.people.util.NowTime;
import com.old.people.util.ShortChainUtils;

@Controller
public class UserController {
	@Autowired
	private UserServices userServices;
	@Autowired
	private PayService payService;
	
	@RequestMapping("/consumer/selectOldpeople")
	@ResponseBody
	public OldPeople selectOldpeople(String account_id) {
		return userServices.selectOldpeople(account_id);
	}

	@RequestMapping("/consumer/updateOldpeople")
	@ResponseBody
	public boolean updateOldpeople(String oldpeople_id, String oldpeople_name, String oldpeople_sex,
			String oldpeople_idcard, String oldpeople_address, String oldpeople_tel, String oldpeople_description,
			String oldpeople_familytel) {
		return userServices.updateOldpeople(oldpeople_id, oldpeople_name, oldpeople_sex, oldpeople_idcard, oldpeople_address, oldpeople_tel, oldpeople_description, oldpeople_familytel);
	}
	
	@RequestMapping("/consumer/getAppraisals")
	@ResponseBody
	public List<Appraisal> getAppraisals(String account_id,Integer page){
		return userServices.getAppraisal(account_id, page);
	}
	
	@RequestMapping("/consumer/getAppraisalPages")
	@ResponseBody
	public Integer getAppraisalPages(String account_id) {
		return userServices.getAppraisalPage(account_id);
	}
	
	@RequestMapping("/consumer/getdiseases")
	@ResponseBody
	public List<Disease> getdiseases(String account_id) {
		return userServices.getdiseases(account_id);
	}
	
	@RequestMapping("/consumer/getOrderServiceServicer")
	@ResponseBody
	public List<OrderServiceServicer> getOrderServiceServicer(String account_id, int page) {
		return userServices.getOrderServiceServicer(account_id, page);
	}

	@RequestMapping("/consumer/getOrderServiceServicerPage")
	@ResponseBody
	public int getOrderServiceServicerPage(String account_id) {
		return userServices.getOrderServiceServicerSum(account_id);
	}

	@RequestMapping("/consumer/updateOrder")
	@ResponseBody
	public boolean updateOrder(String order_id, String order_status) {
		return userServices.updateOrder(order_id, order_status);
	}

	@RequestMapping("/consumer/updateOrders")
	@ResponseBody
	public boolean updateOrders(String order_id) {
		String order_completetime=NowTime.getTime();
		return userServices.updateOrders(order_id, order_completetime);
	}

	@RequestMapping("/consumer/insertAppraisal")
	@ResponseBody
	public boolean insertAppraisal(String appraisal_grade, String appraisal_content,
			String account_id, String order_id) {
		String appraisal_id=ShortChainUtils.getString();
		return userServices.insertAppraisal(appraisal_id, appraisal_grade, appraisal_content, account_id, order_id);
	}
	
	@RequestMapping("/consumer/getServices")
	@ResponseBody
	public List<ServiceServicer> getServices(Integer page) {
		return userServices.getServices(page);
	}

	@RequestMapping("/consumer/getServicePages")
	@ResponseBody
	public int getServicePage() {
		return userServices.getServicePage();
	}

	@RequestMapping("/consumer/selectServices")
	@ResponseBody
	public List<ServiceServicer> selectService(String service_name) {
		return userServices.selectService(service_name);
	}

	@RequestMapping("/consumer/insertOrders")
	@ResponseBody
	public boolean insertOrder(String dateType,
			String order_price, String servicer_id,
			String account_id) {
		String order_id=ShortChainUtils.getString();
		String order_createtime=NowTime.getTime();
		String order_completetime="";
		String order_status="未完";
		String order_scheduledtime="";
		String oldpeople_id=userServices.getOldpeopleId(account_id);
		if(dateType.equals("1")) {
			order_scheduledtime=NowTime.getToday();
		}else {
			order_scheduledtime=NowTime.getTomorry();
		}
		return userServices.insertOrder(order_id, order_createtime, order_scheduledtime, order_completetime, order_status, order_price, servicer_id, oldpeople_id, account_id);
	}
	
	@RequestMapping("/consumer/insertUserFunds")
	@ResponseBody
	public boolean insertUserFunds(String funds_type, String funds_amount,
			String account_id,String servicer_id) {
		String funds_date=NowTime.getTime();
		String funds_id=ShortChainUtils.getString();
		if(account_id.equals("一")) {
			account_id=userServices.getAccount(servicer_id);
		}
		return userServices.insertUserFunds(funds_id, funds_type, funds_amount, funds_date, account_id);
	}
	
	@RequestMapping("/consumer/getServicerAccount")
	@ResponseBody
	public String getServicerAccount(String servicer_id) {
		return userServices.getAccount("165");
	}
	
	@PostMapping(value = "/consumer/alipay")
	public String alipay(String funds_amount) throws AlipayApiException {
		String outTradeNo=ShortChainUtils.getString();
		String body="支付宝支付";
		String subject="社区服务";
		AlipayBean alipayBean = new AlipayBean();
		alipayBean.setOut_trade_no(outTradeNo);
		alipayBean.setSubject(subject);
		alipayBean.setTotal_amount(funds_amount);
		alipayBean.setBody(body);
		return payService.aliPay(alipayBean);
	}
	@PostMapping(value = "/alipay")
	public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
		AlipayBean alipayBean = new AlipayBean();
		alipayBean.setOut_trade_no(outTradeNo);
		alipayBean.setSubject(subject);
		alipayBean.setTotal_amount(totalAmount);
		alipayBean.setBody(body);
		return payService.aliPay(alipayBean);
//		return "jsp/login.jsp";
	}
}
